using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Marlimer.Logical.Comercial;
using Marlimer.Logical.Universal;
using Marlimer.Logical.Sistema;
using Marlimer.Common;
using Marlimer.Comercial.Reportes.Crystals; 

namespace Marlimer.Comercial.Reportes.Formularios
{
    public partial class frm_RepPagos : Form
    {

        lcom_CondicionVenta lcom_condicionventa = new lcom_CondicionVenta();
        luni_TipoDocVta luni_tipodocvta = new luni_TipoDocVta();
        lcom_Transportista lcom_transportista = new lcom_Transportista();
        lcom_Cobrador lcom_cobrador = new lcom_Cobrador();
        luni_TipoPago luni_tipopago = new luni_TipoPago();
        lsis_Usuario lsis_usuario = new lsis_Usuario();
        List<InfoParamRpt> paramsrpt = new List<InfoParamRpt>();
        rptPagosCobranzas RptPagosCobranzas = new rptPagosCobranzas();

        public frm_RepPagos()
        {
            InitializeComponent();
        }

        private void frm_RepPagos_Load(object sender, EventArgs e)
        {

            CargarCombos();

            dtpFecIni.Value = DateTime.Today;
            dtpFecFin.Value = DateTime.Today;
            
            this.Location = new Point(0, 0);
             
        }


        private void CargarCombos()
        {
            Util.LlenarDataCombo(lcom_condicionventa.getDTCondicionVenta(), cboCondVta, true);
            Util.LlenarDataCombo(luni_tipodocvta.getDTTipoDocvta(), cboTipoDocVta, true);
            Util.LlenarDataCombo(luni_tipopago.getDTTipoPago(2), cboTipoPago, true);    //todos los tipos de pago
            Util.LlenarDataCombo(lcom_transportista.getDTTransportista(), cboTransportista, true);
            Util.LlenarDataCombo(lcom_cobrador.getDTCobrador(), cboCobrador, true);
            Util.LlenarDataCombo(lsis_usuario.getDTUsuario(), cboUsuario, true);
        }


        private void cboGeneral_KeyPress(object sender, KeyPressEventArgs e)
        {
            Util.validaEnter(e.KeyChar);
        }

        private void txtGeneral_Leave(object sender, EventArgs e)
        {
            TextBox txt = (TextBox)sender;
            String value = "";
            int valuenum = -1;
            String nametxt = txt.Name;
            object newval = null;

            if (txt.Text.Trim() != "")
            {
                value = txt.Text;
                valuenum = Convert.ToInt32(Util.ToValor(txt.Text));
            }

            switch (nametxt)
            {
                case "txtCodTransportista":
                    {
                        cboTransportista.SelectedValue = valuenum;
                        newval = cboTransportista.SelectedValue;  
                        break;
                    }
                case "txtCodCobrador":
                    {
                        cboCobrador.SelectedValue = valuenum;
                        newval = cboCobrador.SelectedValue;
                        break;
                    }
                case "txtCodUsuario":
                    {
                        cboUsuario.SelectedValue = valuenum;
                        newval = cboUsuario.SelectedValue;  
                        break; 
                    }
            }
            txt.Text = Convert.ToString(newval);
        }

        private void cboGeneralDT_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cbo = (ComboBox)sender;
            String valor = "";
            String namecbo = cbo.Name;
            //validamos que el indice seleccionado sea mayor a -1
            if (cbo.SelectedIndex > -1 && cbo.SelectedValue.ToString() != "System.Data.DataRowView")
            {
                valor = Convert.ToString(cbo.SelectedValue);
            }

            switch (namecbo)
            {
                case "cboTransportista":
                    {
                        txtCodTransportista.Text = valor;
                        break;
                    }
                case "cboCobrador":
                    {
                        txtCodCobrador.Text = valor; 
                        break;
                    }
                case "cboUsuario":
                    {
                        txtCodUsuario.Text = valor; 
                        break;
                    }
            }

        }

        private void txtGeneralSelect_Enter(object sender, EventArgs e)
        {
            Util.selectAllText(((TextBox)sender));
        }


        private void txtGeneralEntero_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = Util.validaTecla(e.KeyChar, "Entero", ((TextBox)sender).Text, true);
        }

        private void tstComandos_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            int indice = tstComandos.Items.IndexOf(e.ClickedItem);
            switch (indice)
            {
                case 0:     //nuevo
                    {
                        nuevaBusqueda();
                        break;
                    }
                case 1:     //imprimir o mostrar
                    {
                        CargarReporte();
                        break;
                    }
                case 2:
                    {
                        Util.exportReporte(RptPagosCobranzas);
                        break;
                    }
            }
        }

        private void nuevaBusqueda()
        {
            CargarCombos();
            txtCodCliente.Text = "";
            cboTipoDocVta.Focus();  
        }

        private void CargarReporte()
        {
            //verificamos que se ingresen todos los datos.
            //establecemos los parametros
            paramsrpt.Clear();  
            paramsrpt.Add(new InfoParamRpt("@dfec_ini", dtpFecIni.Value));
            paramsrpt.Add(new InfoParamRpt("@dfec_fin", dtpFecFin.Value));
            paramsrpt.Add(new InfoParamRpt("@ccod_trans", Convert.ToInt32(cboTransportista.SelectedValue)));
            paramsrpt.Add(new InfoParamRpt("@ccod_cobra", Convert.ToInt32(cboCobrador.SelectedValue)));
            paramsrpt.Add(new InfoParamRpt("@ccod_cli", Convert.ToInt32(Util.ToValor(txtCodCliente.Text))));
            paramsrpt.Add(new InfoParamRpt("@ccod_user", Convert.ToInt32(cboUsuario.SelectedValue)));
            paramsrpt.Add(new InfoParamRpt("@ctip_docvta", Convert.ToInt32(cboTipoDocVta.SelectedValue)));
            paramsrpt.Add(new InfoParamRpt("@ccod_condvta", Convert.ToInt32(cboCondVta.SelectedValue)));
            paramsrpt.Add(new InfoParamRpt("@ccod_tipopago", Convert.ToInt32(cboTipoPago.SelectedValue)));

            //verificar segun tipo de formato    
            //llamar al rpt segun el tipo de documento
            try
            {
                crpReporte.ReportSource = Util.setRptParams(RptPagosCobranzas, paramsrpt);
                tblExportar.Enabled = true;
            }
            catch (Exception ex)
            {
                tblExportar.Enabled = false; 
            }
        }

    }
}